
import * as types from './mutation-types';
import util from '../utils/util';
import messages from '../i18n/langs/index'
export default {

	[types.INIT_GLOBAL_PROPERTYS](state,props) {
		props.forEach(payload => {
			state.global[payload.key] = payload.value
	 	});
		if(localStorage.map){
			state.map.type = JSON.parse(localStorage.map).type
		}
	},

	[types.INIT_USER_INFO](state,prop) {
		state.user = prop;
	},
	[types.INIT_USER_PERMISSIONS](state,prop) {
		state.permissions = prop
	},
	[types.INIT_USER_DEVICES](state,props) {
		var devices = {
            selectId:0,
            index:{},
            list:[]
          };
		var onlineState = {
				 online:0,
				 offline:0,
				 notactive:0
			 }
				for(var i=0;i<props.length;i++){
					devices.index[props[i].id] = i;
					devices.list[i] = props[i];
					devices.list[i].motion = 0;
					devices.list[i].marker = {
														id:props[i].id,
														label:props[i].name,
														coord:[],
														color:props[i].status==0?'#cccccc':'#aaff56',
														angle:0,
														visible:true
												}
					if(props[i].status==1){
	          onlineState.online++
	         }else if(props[i].status==0){
	           onlineState.offline++
	         }else{
	           onlineState.notactive++
	         }
				}

		state.devices = devices;
		state.onlineState = onlineState
	},
	[types.UPDATE_DEVICE_POSITION](state, positions) {

		for(var i=0;i<positions.length;i++){

				var index = state.devices.index[positions[i].deviceId]
				if(typeof(index) == "undefined"){
					continue;
				}
				state.devices.list[index].marker.coord = [positions[i].longitude,positions[i].latitude];
				state.devices.list[index].position = positions[i];
				state.devices.list[index].marker.angle = positions[i].course;
				state.devices.list[index].lastUpdate = positions[i].time;
				//state.user.devices.list[index].attributes = JSON.parse(positions[i].attributes);
				state.devices.list[index].marker.color = state.devices.list[index].position.alarm?'#ff0000':state.devices.list[index].marker.color;
				//如果运动状态发生改变
				if(state.devices.list[index].motion!=util.motion(positions[i].time)){
					state.devices.list[index].motion = util.motion(positions[i].time);
				}
				if((state.devices.selectId==positions[i].deviceId)){
					var current = positions[i];
					var attributes = JSON.parse(current.attributes);
					if(state.map.isTrackCenter){
						state.map.center = [current.longitude,current.latitude]
					}
					state.popup.coord=[current.longitude,current.latitude];
					state.popup.circle={
						 visible:current.mode!=0?true:false,
						 radius:current.accuracy||0,
						 strokeColor:'#409EFF',
						 fillColor:'rgba(64,158,255, 0.2)'
					 }

					 state.popup.topRight = current.mode==0?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-gps"></use></svg>'+(!attributes.sat? "" : " <span style=\"font-size : 0.3em;\">" + attributes.sat + "</span>"):current.mode==1?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-jizhan"></use></svg>'+(!current.accuracy? "" : "<span style=\"font-size : 0.3em;\">" + current.accuracy + "m</span>"):'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-wifi"></use></svg>'
					 state.popup.content = [
		 				messages[state.global.lang].map.status+':'+(state.devices.list[index].motion==1?parseInt(current.speed)+'km/h ('+util.formartCourse(current.course)+')':messages[state.global.lang].map.stop+'('+util.beforeTime(current.time)+')')+(attributes.alarm?'['+attributes.alarm+']':''),
		 				messages[state.global.lang].map.mileage+':'+(attributes.todaytrip?attributes.todaytrip:0)+'/'+(attributes.trip?attributes.trip:0)+' km',
		 				messages[state.global.lang].map.time+':'+util.dateFormat(current.time,"YYYY-MM-DD HH:mm:ss")
		 			];
					if(attributes){
						let footer = "";
						footer=footer+(attributes.battery?"<div  data-charge=\"" + (Math.round(attributes.battery / 100 * 6)) + "\" class=\"battery\"></div> ":'')
						footer=footer+(attributes.voltage?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-chatou"></use></svg>'+attributes.voltage+'v':'')
						footer=footer+(attributes.ignition==undefined?'':''+(attributes.ignition?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg>'+attributes.ignition:'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg>'+attributes.ignition))
						footer=footer+ (attributes.defense==undefined?'':''+(attributes.defense?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockclose"></use></svg>':'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockopen"></use></svg>'))
						footer=footer+(!attributes.temp?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-temp"></use></svg>'+attributes.temp+'℃</span>')
						footer=footer+(!attributes.turn?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-jiaobanche"></use></svg>'+attributes.turn+'</span>')
						footer=footer+(!attributes.fuelPercentage?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fuel"></use></svg>'+attributes.fuelPercentage+'%</span>')
						if(footer!=''){
							footer="<hr style='height:1px;border:none;border-top:1px dashed #0066CC;'>"+footer
						}
						state.popup.footer = footer
					}
				}
			}
	},
	[types.UPDATE_DEVICE_STATUS](state, devices) {

		var devstatus = devices[0];
		var index = state.devices.index[devstatus.id];
		if(typeof(index) == "undefined"){
			return;
		}
		state.devices.list[index].status = devstatus.status;
		if(devstatus.status==0||devstatus.status==1){
					state.devices.list[index].marker.color = (devstatus.status==0?'#cccccc':'#aaff56');
		}
		state.devices.list[index].lastUpdate = devstatus.time;
		if(state.devices.list[index].position){
			state.devices.list[index].motion = util.motion(state.devices.list[index].position.time);
		}else{
			state.devices.list[index].motion = 1;
		}
		//更新跟踪设备的气泡信息
		if(state.devices.selectId==devstatus.id){
			 var attributes = JSON.parse(state.devices.list[index].position.attributes)
			state.popup.content = [
			 messages[state.global.lang].map.status+':'+(state.devices.list[index].motion==1?parseInt(state.devices.list[index].position.speed)+'km/h ('+util.formartCourse(state.devices.list[index].position.course)+')':messages[state.global.lang].map.stop+'('+util.beforeTime(state.devices.list[index].position.time)+')')+(attributes.alarm?'['+attributes.alarm+']':''),
			 messages[state.global.lang].map.mileage+':'+(attributes.todaytrip?attributes.todaytrip:0)+'/'+(attributes.trip?attributes.trip:0)+' km',
			 messages[state.global.lang].map.time+':'+util.dateFormat(state.devices.list[index].position.time,"YYYY-MM-DD HH:mm:ss")
		 ];
		}
		window.f7.notification.create({
												icon: '<i class="f7-icons">volume</i>',
												title: messages[state.global.lang].map.deviceEvent,
												titleRightText: messages[state.global.lang].map.now,
												subtitle: state.devices.list[index].name,
												text:messages[state.global.lang].events[state.status[devstatus.status]],
												//text: devstatus.status==1?messages[state.global.lang].events.online:devstatus.status==0?messages[state.global.lang].events.offline:devstatus.status==9?messages[state.global.lang].events.alarm:messages[state.global.lang].events.unknown,
												// closeOnClick: true,
											  closeTimeout: 5000,
											}).open();
	},
	[types.SET_SELECT_DEVICE_ID](state, deviceId) {

		state.map.mode = 1;
		state.devices.selectId = deviceId;
		var index = state.devices.index[deviceId];
		let device = state.devices.list[index];
		if(device.position){

			var attributes = JSON.parse(device.position.attributes);
			state.popup.visible=true;
			state.popup.coord = [device.position.longitude,device.position.latitude];
			state.popup.header=device.name;
			//state.popup.topRight = device.position.mode==0?'GPS':device.position.mode==1?'基站':'WIFI'
			state.popup.topRight = device.position.mode==0?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-gps"></use></svg>'+(!attributes.sat? "" : " <span style=\"font-size : 0.3em;\">" + attributes.sat + "</span>"):device.position.mode==1?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-jizhan"></use></svg>'+(!device.position.accuracy? "" : "<span style=\"font-size : 0.3em;\">" + device.position.accuracy + "m</span>"):'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-wifi"></use></svg>'
			state.popup.content = [
				messages[state.global.lang].map.status+':'+(device.motion==1?parseInt(device.position.speed)+'km/h ('+util.formartCourse(device.position.course)+')':messages[state.global.lang].map.stop+'('+util.beforeTime(device.position.time)+')')+(attributes.alarm?'['+messages[state.global.lang].map.alarm[attributes.alarm]+']':''),
				messages[state.global.lang].map.mileage+':'+(attributes.todaytrip?attributes.todaytrip:0)+'/'+(attributes.trip?attributes.trip:0)+' km',
				messages[state.global.lang].map.time+':'+util.dateFormat(device.position.time,"YYYY-MM-DD HH:mm:ss")
			];

			if(attributes){
				let footer = "";
				footer=footer+(attributes.battery?"<div data-charge=\"" + (Math.round(attributes.battery / 100 * 6)) + "\" class=\"battery\"></div> ":'')
				footer=footer+(attributes.voltage?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-chatou"></use></svg>'+attributes.voltage+'v':'')
				footer=footer+(attributes.ignition==undefined?'':''+(attributes.ignition?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg>'+attributes.ignition:'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fadongji"></use></svg>'+attributes.ignition))
				footer=footer+ (attributes.defense==undefined?'':''+(attributes.defense?'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockclose"></use></svg>':'<svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-lockopen"></use></svg>'))
				footer=footer+(!attributes.temp?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-temp"></use></svg>'+attributes.temp+'℃</span>')
				footer=footer+(!attributes.turn?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-jiaobanche"></use></svg>'+attributes.turn+'</span>')
				footer=footer+(!attributes.fuelPercentage?'':'<span><svg aria-hidden="true"  class="bubble-icon"><use xlink:href="#icon-fuel"></use></svg>'+attributes.fuelPercentage+'%</span>')
				if(footer!=''){
					footer="<hr style='height:1px;border:none;border-top:1px dashed #0066CC;'>"+footer
				}
				state.popup.footer = footer
			}


			if(state.map.isTrackCenter){
				   state.map.zoom = 16
					 state.map.center = [device.position.longitude,device.position.latitude]
			}

		}else{
			window.f7.toast.create({
				 text: messages[state.global.lang].map.invalidLocation,
				 closeTimeout: 2000,
			 }).open();
		}

	},
	[types.SET_UNREAD_COUNT](state,prop) {
		state.events.unReadCount = prop
	},
	[types.SET_MAP_VALUE](state,props) {
			props.forEach(payload => {
				state.map[payload.key] = payload.value
		 });
		},

[types.USER_IS_SHOW_BUBBLE](state) {

		if(state.popup.visible){
			state.popup.visible = false
		}else{
			state.popup.visible = true
		}
	},
[types.INIT_STATIC_DATA](state, prop) {
	state.dict.deviceTypes = prop.deviceTypes
	state.dict.commandViews = prop.commands
	state.dict.commandIdsByType = prop.ids
},

};
